package com.atguigu.flow.demo;

import com.atguigu.flow.config.TaxCloudConfig;
import com.atguigu.flow.entity.vo.Module_Field;
import com.atguigu.flow.mapper.SqlMapper;
import com.atguigu.flow.service.IwInstanceService;
import com.atguigu.flow.utils.GetFlowModule_field_util;
import com.atguigu.flow.utils.RSAdemo;
import com.atguigu.flow.utils.Workflow;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.IOException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
@MapperScan("com.atguigu.flow.mapper")

public
class flowtest {


    @Autowired
    SqlMapper sqlMapper;
    @Autowired
    GetFlowModule_field_util getFlowModule_field_util;
    @Autowired
    TaxCloudConfig taxCloudConfig;
    @Autowired
    IwInstanceService iwInstanceService;
    @Test
    public void get_resourcepath() throws Exception {
        File directory = new File("src/main/resources/key");
        String reportPath = directory.getCanonicalPath();
        String algorithm = "RSA";
        RSAdemo.generateKeyToFile(algorithm, reportPath+"\\a.pub", reportPath+"\\a.pri");
        //System.out.println(reportPath);


    }
    @Test
    public void Encryption() throws Exception {
        String input = "{ \"billNotifyNo\": \"A03QC1390-001\", \"orderNum\": \"A03QC1390-zjnb969\"}";
        // 创建密钥对
        //KeyPairGenerator//:密钥对生成器对象
        String algorithm = "RSA";
        File directory = new File("src/main/resources");
        String reportPath = directory.getCanonicalPath();
        //System.out.println(reportPath+taxCloudConfig.getPrivatkeypath());
        //System.out.println(reportPath+taxCloudConfig.getPublickeypath());
        File file=new File(reportPath+taxCloudConfig.getPrivatkeypath());
        if (file.exists()) { System.out.println("文件已存在"); }
        else { System.out.println("文件不存在");}
        //保存公钥和私钥 generateKeyToFile(algorithm, "a.pub", "a.pri");
//        // 读取私钥
        PrivateKey privateKey = RSAdemo.getPrivateKey(reportPath+taxCloudConfig.getPrivatkeypath(), algorithm);
        //System.out.println(privateKey);
        // 读取公钥key
        PublicKey publicKey = RSAdemo.getPublicKey(reportPath+taxCloudConfig.getPublickeypath(), algorithm);
        //System.out.println(publicKey);

        //用私钥加密
        System.out.println("---------用私钥加密，公钥解密--------");
        String s = RSAdemo.encryptRSA(algorithm, privateKey, input);
        System.out.println(s);

        s="DuGh1GvEPo7aErWCNhWxCpJZMUIcYmkiFQO2u4UZ/ScHTD+xPcwablJeNN6VSdr71C/EHrTRCWEw\nJrXY/Ocl1bcrGy0TtmL8hUQ2nlAU8BtX8bXKZaOrk1G+MiU9oDwzZ1dmE93V0tyJnaXELBY4Gkma\n2EyTw82fJY1UarJ0y50/NSSpPULdr8tcCWpnBweHSLS+C62p+/k3l/kjcHAsIIpu1wnkrF2YZNfx\nR+C8ichxNjO4vSHW+lDy2LVqHQrm5f0isP6nlfn+04g7iFA0UnxXZR0rltNqVMPXdas1T9IBfdwj\ne0H1uSXwwRNLwBKTzT0aXUNJNGiShTVFaH/x5g==";
        //用公钥解密
        String s1 = RSAdemo.decryptRSA(algorithm, publicKey, s);
        System.out.println(s1);


        //用私钥加密
        System.out.println("---------用公钥加密，私钥解密--------");
         s = RSAdemo.encryptRSA(algorithm, publicKey, input);
        System.out.println(s);
        //用公钥解密
         s1 = RSAdemo.decryptRSA(algorithm, privateKey, s);
        System.out.println(s1);


    }
    @Test
    public void getflowcon() throws DocumentException {

        String str = "((IStrToFloat(Self.QueryRecordValue('利润率')) < 15)  and(pos('事业四部',Self.QueryRecordValue('部门'))=0))   or((IStrToFloat(Self.QueryRecordValue('利润率')) <=8)and (pos('事业四部',Self.QueryRecordValue('部门'))>0))";
        String recordid = "3C6A52C9-28A4-4883-B076-789D25EF2656";
        String modulename = "洽谈";
        Workflow.module_arraylist = Workflow.getmodle_fieldlist("洽谈");
        //System.out.println( Workflow.module_arraylist);
        // getFlowModule_field_util.getflowcondition(str,recordid,modulename);
        //System.out.println("00");
        Boolean con_result = getFlowModule_field_util.getflowcondition(str, recordid, modulename);
        //System.out.println(con_result);

    }
@Test
    public void changeflowuser() throws DocumentException {
        String recordid="234677A7-770F-49FE-AB16-31EC7190FB40";
         String modulename="付款申请";
    String olduser="应志骏";
    String newuser="leechen";
//    String newuser="陈海闽";
    Boolean isupdate=iwInstanceService.changeusr(recordid,modulename,olduser,newuser);
    System.out.println(isupdate);
        
}

}

